package com.lintf.devdemo.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

/**
 * @ system:Window
 * @ author:lin tingfeng
 * @ data:2022/10/19-17:54
 * @ package_name:com.lintf.devdemo.utils
 * @ project_name:DevDemo
 * @ desc:Base64工具类
 */
public class Base64 {

    public static boolean Base64ToImage(String imgStr,String imgFilePath) {
        try {
            // Base64解码
            byte[] b = new BASE64Decoder().decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 图片转base64字符串
     * @param imgFile 图片路径
     * @return
     */
    public static String imageToBase64Str(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    /**
     * 将图片转化为Base64图片流
     * @param fileUploadPath
     * @return
     */
    public static String readImageInputStream(String fileUploadPath) {
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        try {
            String fileLocation = fileUploadPath;
            String mediaType = fileLocation.substring(fileLocation.lastIndexOf(".") + 1);
            File imageFile = new File(fileLocation);
            boolean exists = imageFile.exists();
            if (exists) {
                inputStream = new FileInputStream(fileLocation);
                outputStream = new ByteArrayOutputStream();
                int len;
                byte[] buf = new byte[1024];
                while ((len = inputStream.read(buf)) != -1) {
                    outputStream.write(buf, 0, len);
                }
                outputStream.flush();
                byte[] data = outputStream.toByteArray();
                BASE64Encoder encoder = new BASE64Encoder();
                String encodeStr = encoder.encode(data);
                encodeStr = "data:image/" + mediaType + ";base64," + encodeStr;
                System.err.println(encodeStr);
                return encodeStr;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
